<template>
  <div class="mod-jmcourse">
    <h3>必选课程列表</h3>
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList1()">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="任务名或教师名等" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList1()">查询</el-button>
        <!-- <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList" ref="table"
      border
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        :selectable="checkSelectable1"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="courseName"
        header-align="center"
        align="center"
        label="任务名称">
      </el-table-column>
      <el-table-column
        prop="courseCode"
        header-align="center"
        align="center"
        label="任务代码">
      </el-table-column>
      <el-table-column
        prop="courseTeacher"
        header-align="center"
        align="center"
        label="教师姓名">
      </el-table-column>
      <el-table-column
        prop="courseAddress"
        header-align="center"
        align="center"
        label="上课地点">
      </el-table-column>
      <el-table-column
        prop="courseDate"
        header-align="center"
        align="center"
        label="上课时间">
      </el-table-column>
      <el-table-column
        prop="courseTime"
        header-align="center"
        align="center"
        label="学时">
      </el-table-column>
      <!-- <el-table-column
        prop="coursePersions"
        header-align="center"
        align="center"
        label="容量">
      </el-table-column>
      <el-table-column
        prop="courseLess"
        header-align="center"
        align="center"
        label="余量">
      </el-table-column> -->

      <el-table-column
        prop="courseHome"
        header-align="center"
        align="center"
        label="开课学院">
      </el-table-column>

      <el-table-column
        header-align="center"
        align="center"
        v-if="isAuth('sys:jmcourse:update')"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:jmcourse:update')" type="text" size="small" @click="exportStu(scope.row.id)">导出</el-button>
          <el-button v-if="isAuth('sys:jmcourse:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="isAuth('sys:jmcourseuser:stus')" type="text" size="small" @click="stuListHandle(scope.row.id)">查看选课学生</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle1"
      @current-change="currentChangeHandle1"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <h3>任选课程列表</h3>
    <el-form :inline="true" :model="searchForm2" @keyup.enter.native="getDataList2()">
      <el-form-item>
        <el-input v-model="searchForm2.name" placeholder="任务名或教师名等" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList2()">查询</el-button>
        <el-button type="primary" @click="saveData()">提交选中任务</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList2" ref="table2"
      border
      @selection-change="selectionChangeHandle2"
      style="width: 100%;">
      <el-table-column
        type="selection"
        :selectable="checkSelectable2"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="courseName"
        header-align="center"
        align="center"
        label="任务名称">
      </el-table-column>
      <el-table-column
        prop="courseCode"
        header-align="center"
        align="center"
        label="任务代码">
      </el-table-column>
      <el-table-column
        prop="courseTeacher"
        header-align="center"
        align="center"
        label="教师姓名">
      </el-table-column>
      <el-table-column
        prop="courseAddress"
        header-align="center"
        align="center"
        label="上课地点">
      </el-table-column>
      <el-table-column
        prop="courseDate"
        header-align="center"
        align="center"
        label="上课时间">
      </el-table-column>
      <el-table-column
        prop="courseTime"
        header-align="center"
        align="center"
        width="50"
        label="学时">
      </el-table-column>
      <el-table-column
        prop="coursePersions"
        header-align="center"
        align="center"
        width="50"
        label="容量">
      </el-table-column>
      <el-table-column
        prop="courseLess"
        header-align="center"
        align="center"
        width="50"
        label="余量">
      </el-table-column>

      <el-table-column
        prop="courseHome"
        header-align="center"
        align="center"
        label="开课学院">
      </el-table-column>

      <el-table-column
        header-align="center"
        align="center"
        v-if="isAuth('sys:jmcourse:update')"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="showStudentHandle2(scope.row)">
            <el-button v-if="isAuth('sys:jmcourse:update')" type="text" size="small" @click="exportStu(scope.row.id)">导出</el-button>
            <el-button v-if="isAuth('sys:jmcourse:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
            <el-button v-if="isAuth('sys:jmcourseuser:stus')" type="text" size="small" @click="stuListHandle2(scope.row.id)">查看选课学生</el-button>
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle2"
      @current-change="currentChangeHandle2"
      :current-page="pageIndex2"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize2"
      :total="totalPage2"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 弹窗,学生选课列表 -->
    <stu-list-bx v-if="stuListVisable1" ref="stuList1" @refreshDataList="getDataList1"></stu-list-bx>
    <stu-list v-if="stuListVisable2" ref="stuList2" @refreshDataList="getDataList2"></stu-list>
  </div>
</template>

<script>
  import AddOrUpdate from './jmcourse-add-or-update'
  import StuListBx from './stu-list-bix.vue'
  import StuList from './stu-list.vue'

  export default {
    data () {
      return {

        addOrUpdateVisible: false,
        stuListVisable1: false,
        stuListVisable2: false,

         // 必选任务数据
        searchForm: {
          name: ''
        },
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataList: [],

        // 任选任务数据
        searchForm2: {
          name: ''
        },
        pageIndex2: 1,
        pageSize2: 10,
        totalPage2: 0,
        dataList2: [],
        havaSelect: [], // 已经选择的任选任务
        dataListSelections2: []
      }
    },
    components: {
      AddOrUpdate,
      StuListBx,
      StuList
    },
    activated () {
      this.getDataList1()
      this.getDataList2()
    },
    mounted () {
      // this.$refs.table.toggleRowSelection(this.dataList, true)
    },
    methods: {
      checkSelectable1 (row) {
        return false
      },
      checkSelectable2 (row) {
        if (row.courseLess <= 0) {
          return false
        }
        return true
      },
      // 获取数据列表
      getDataList1 () {
        this.$http({
          url: '/sys/select/mustList',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.searchForm.name,
            'moduleId': '2'
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
            // 进入页面默认勾选
            this.$nextTick(() => {
              this.dataList.forEach((item) => {
                this.$refs.table.toggleRowSelection(item, true)
              })
            })
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },

      // 获取数据列表
      getDataList2 () {
        this.$http({
          url: '/sys/select/mayList',
          method: 'get',
          params: {
            'page': this.pageIndex2,
            'limit': this.pageSize2,
            'name': this.searchForm2.name,
            'moduleId': '2'
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            var self = this
            this.dataList2 = data.page.records
            this.totalPage2 = data.page.total
            this.havaSelect = data.havaSelect
            // 进入页面默认勾选
            this.$nextTick(() => {
              this.dataList2.forEach((item) => {
                if (self.havaSelect.includes(item.id)) {
                  this.$refs.table2.toggleRowSelection(item, true)
                }
              })
            })
          } else {
            this.dataList2 = []
            this.totalPage2 = 0
          }
        })
      },

      saveData () {
        this.$http({
          url: '/sys/select/save',
          method: 'post',
          params: {
            'moduleId': '2',
            'courseIds': this.dataListSelections2.toString()
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.visible = false
            this.dataListSelections2 = [] // 操作成功情况数组
            this.getDataList2() // 刷新列表
            this.$message({
              message: '保存成功！',
              type: 'success',
              duration: 1500
            })
          } else {
            this.visible = false
            this.$message({
              message: '保存失败！',
              type: 'success',
              duration: 1500
            })
          }
        })
      },
      // 查看必选课学生信息
      showStudentHandle () {
      },
      // 查看任选课学生信息
      showStudentHandle2 () {
      },
      // 每页数
      sizeChangeHandle1 (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList1()
      },
      sizeChangeHandle2 (val) {
        this.pageSize2 = val
        this.pageIndex2 = 1
        this.getDataList2()
      },
      // 当前页
      currentChangeHandle1 (val) {
        this.pageIndex = val
        this.getDataList1()
      },
      currentChangeHandle2 (val) {
        this.pageIndex2 = val
        this.getDataList2()
      },
      // 多选
      selectionChangeHandle2 (val) {
        var self = this
        if (val.length > 0) {
          val.forEach((item) => {
            self.dataListSelections2.push(item.id)
          })
        } else {
          self.dataListSelections2 = []
        }
      },
      // 查看学生列表
      stuListHandle (id) {
        console.log(id)
        this.stuListVisable1 = true
        this.$nextTick(() => {
          this.$refs.stuList1.init(id, true)
        })
      },
      stuListHandle2 (id) {
        console.log(id)
        this.stuListVisable2 = true
        this.$nextTick(() => {
          this.$refs.stuList2.init(id, true)
        })
      },
      exportStu (id) {
        let url =
        this.$http.BASE_URL +
        `/course/exportStu?courseId=${id}&token=${this.$cookie.get('token')}`
        window.open(url)
      },
      // 删除
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/sys/jmcourse/delete',
            method: 'post',
            data: ids
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>
